package com.study.common

import com.study.common.event.Constants
import com.study.common.event.OtherMessage
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus

object EventBusUtil {
    private val event: EventBus = EventBus.getDefault()

    fun sendEvent(message: Any, isSticky: Boolean) {
        if (isSticky) event.postSticky(message)
        else event.post(message)
    }

    @JvmStatic
    fun sendEvent(message: Any) {
        event.post(message)
    }

    @JvmStatic
    fun sendEventDelayed(message: Any, time: Long) {
        CoroutineScope(Dispatchers.IO).launch{
            delay(1000)
            sendEvent(OtherMessage(Constants.CONNECTION_SUCCESS))
        }
    }

    fun registerEvent(obj: Any) {
        event.register(obj)
    }

    fun unregisterEvent(obj: Any) {
        event.unregister(obj)
    }
}
